package com.x.mvp.widget;

import android.app.DialogFragment;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.widget.TextView;

import com.x.mvp.Constants;
import com.x.mvp.R;

public class FindVersionDialogFragment extends DialogFragment {
    public static final String KEY_FORCE_UPDATE_TYPE = "key_force_update_type";

    public static final String VERSION_NUM = "version_num";
    public static final String VERSION_CONTENT = "version_content";
    public static final String DOWNLOAD_URL = "download_url";
    private View mContentView;
    private TextView mVersionTitle;
    private TextView mVersionContent;
    private int mForceUpdate;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        getDialog().requestWindowFeature(Window.FEATURE_NO_TITLE);//把dialog的标题栏去掉
        if (mContentView == null) {
            mContentView = inflater.inflate(R.layout.fragment_update_version_dialog, container);
            mVersionTitle = (TextView) mContentView.findViewById(R.id.dialog_version_num);
            mVersionContent = (TextView) mContentView.findViewById(R.id.dialog_version_content);
            if (mForceUpdate == Constants.UpdateType.FORCE_UPDATE) {
                mContentView.findViewById(R.id.dialog_version_cancel).setVisibility(View.GONE);
            }
            initListener();
        }
        mVersionTitle.setText("v" + getArguments().getString(VERSION_NUM));
        mVersionContent.setText(getArguments().getString(VERSION_CONTENT));
        return mContentView;
    }

    @Override
    public void onCreate(Bundle icicle) {
        super.onCreate(icicle);
        mForceUpdate = getArguments().getInt(KEY_FORCE_UPDATE_TYPE);
        if (mForceUpdate == Constants.UpdateType.FORCE_UPDATE) {
            this.setCancelable(false);// 设置点击屏幕Dialog不消失
            setStyle(DialogFragment.STYLE_NORMAL, 0);
        }
    }

    public void initListener() {
        mContentView.findViewById(R.id.dialog_version_cancel).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                getDialog().dismiss();
            }
        });
        mContentView.findViewById(R.id.dialog_version_update).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Uri uri = Uri.parse(getArguments().getString(DOWNLOAD_URL));
                Intent intent = new Intent();
                intent.setData(uri);
                intent.setAction(Intent.ACTION_VIEW);
                startActivity(intent);
            }
        });
    }
}
